﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using ZG_member.BLL;
using System.IO;
using System.Configuration;
using Microsoft.Win32;
using System.Net;

namespace ZG_member
{
    public partial class LoginForm : Form
    {

        public LoginForm()
        {
            InitializeComponent();
        }
        MainForm mainform;
        public static bool shuaxing = true;
        public LoginForm(int i,MainForm main)
        {
            InitializeComponent();
            mainform = main;
            this.ControlBox = false;
            this.Text = "系统锁屏";
        }
        public static int go = 0;
        public LoginForm(char a,MainForm main)
        {
            InitializeComponent();
            mainform = main;
            this.ControlBox = false;
            this.Text = "更换用户";
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"]; 
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
            this.Opacity = 0;
            this.timer1.Start();


            RegistryKey rk = Registry.LocalMachine;
            RegistryKey software = rk.OpenSubKey("SOFTWARE");
            //判断是否注册
            if (!software.GetSubKeyNames().Contains("ruanjianzhuce"))
            {
                this.Visible = false;
                MessageBox.Show("您未权限使用该软件,请联系出产商");
                ZhuCeForm ce = new ZhuCeForm();
                ce.ShowDialog();
            }else if (software.GetSubKeyNames().Contains("ruanjianzhuce"))
            {
                //判断是否过期
                RegistryKey ddd = software.OpenSubKey("ruanjianzhuce");
                DateTime zhuctime =  DateTime.Parse(ddd.GetValue("zhucechenggong").ToString());


                DateTime newtime = zhuctime.AddDays(30);
                if (newtime <= zhuctime)
                {
                    MessageBox.Show("您使用的软件已过期,请联系出产商");
                    ZhuCeForm ce = new ZhuCeForm();
                    ce.ShowDialog();
                }
            }

            

        }

        /// <summary>
        /// 用户名密码对比
        /// </summary>
        /// <param name="sender">密码加密</param>
        /// <param name="e"></param>
        private void btnDetermine_Click(object sender, EventArgs e)
        {
            


            string passWord = txtUserPwd.Text;
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] bt = Encoding.Default.GetBytes(passWord);
            bt = md5.ComputeHash(bt);
            passWord = BitConverter.ToString(bt).Replace("-", "");

            Model.operatorInfo opInfoMod = new Model.operatorInfo();
            operatorInfo opInfo = new operatorInfo();
            try
            {
                opInfoMod = opInfo.GetModelList("username='" + txtUserName.Text.Trim() + "'")[0];
            }
            catch
            {
                MessageBox.Show("用户名，密码错误！");
                return;
            }
            if (this.Text == "用户登陆")
            {
                if (passWord == opInfoMod.password)
                {
                    MainForm.logintime = DateTime.Now;
                    MainForm m = new MainForm();
                    MainForm.Operator = this.txtUserName.Text;
                    
                    go = 1 ;

                    


                    this.Close();
                    return;
                }
            }
            
            if (passWord == opInfoMod.password)
            {
                Model.usersystemnode us = new Model.usersystemnode();
                us.username = MainForm.Operator;
                us.loginTime = MainForm.logintime;
                us.leaveTime = DateTime.Now;
                usersystemnode u = new usersystemnode();
                u.Add(us);
                MainForm.logintime = DateTime.Now;
                MainForm.Operator = this.txtUserName.Text;
                //go = 1;
                shuaxing = true;
                this.Close();
            }
            else
            {
                MessageBox.Show("用户名，密码错误！");
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (this.Text == "用户登陆" | this.Text == "更换用户")
            {
                this.Close();
                shuaxing = false;
            }
            else
            {
                this.Close();
                shuaxing = false;
            }
        }
        int flag = 0;
        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //timer开启
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        

        private void timer2_Tick_1(object sender, EventArgs e)
        {
            //timer关闭
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
                //mainform.TxtCardId.Focus();
            }
        }

        
    }
}
